package io.renren.modules.sys.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.exception.RRException;
import io.renren.common.pojo.Query;
import io.renren.common.pojo.R;
import io.renren.common.util.Constant.MenuType;
import io.renren.modules.sys.entity.SysMenuEntity;
import io.renren.modules.sys.pojo.SysMenuNavVO;
import io.renren.modules.sys.service.SecurityService;
import io.renren.modules.sys.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 系统菜单
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年10月27日 下午9:58:15
 */
@RestController
@RequestMapping("/sys/menu")
@Api(description = "菜单管理接口")
public class SysMenuController extends BaseSysController {
	@Autowired
	private SysMenuService sysMenuService;
	@Autowired
	private SecurityService securityService;

	/**
	 * 导航菜单
	 */
	@GetMapping("/nav")
	@ApiOperation("获取导航菜单列表")
	public R<SysMenuNavVO> nav(){
		SysMenuNavVO vo = new SysMenuNavVO();

		List<SysMenuEntity> menuList = sysMenuService.getUserMenuList(getUserId());
		vo.setMenuList(menuList);

		Set<String> permissions = securityService.getUserPermissions(getUserId());
		vo.setPermissions(permissions);

		return R.ok(vo);
	}
	
	/**
	 * 所有菜单列表
	 */
	@GetMapping("/list")
	@PreAuthorize("hasAuthority('sys:menu:list')")
	@ApiOperation("获取菜单列表")
	public List<SysMenuEntity> list(){
		return sysMenuService.queryList(new Query());
	}
	
	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@GetMapping("/select")
	@PreAuthorize("hasAuthority('sys:menu:select')")
	@ApiOperation("选择菜单(添加、修改菜单)")
	public R<List<SysMenuEntity>> select(){
		//查询列表数据
		List<SysMenuEntity> menuList = sysMenuService.queryNotButtonList();
		
		//添加顶级菜单
		SysMenuEntity root = new SysMenuEntity();
		root.setMenuId(0L);
		root.setName("一级菜单");
		root.setParentId(-1L);
		root.setOpen(true);
		menuList.add(root);
		
		return R.ok(menuList);
	}
	
	/**
	 * 菜单信息
	 */
	@GetMapping("/info/{menuId}")
	@PreAuthorize("hasAuthority('sys:menu:info')")
	@ApiOperation("获取菜单信息")
	public R<SysMenuEntity> info(@ApiParam("菜单ID") @PathVariable("menuId") Long menuId){
		SysMenuEntity menu = sysMenuService.queryObject(menuId);
		return R.ok(menu);
	}
	
	/**
	 * 保存
	 */
	@SysLog("保存菜单")
	@PostMapping("/")
	@PreAuthorize("hasAuthority('sys:menu:save')")
	@ApiOperation("保存菜单")
	public R save(@ApiParam("菜单对象") @RequestBody SysMenuEntity menu){
		//数据校验
		verifyForm(menu);
		
		sysMenuService.save(menu);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改菜单")
	@PutMapping("/")
	@PreAuthorize("hasAuthority('sys:menu:update')")
	@ApiOperation("修改菜单")
	public R update(@ApiParam("菜单对象") @RequestBody SysMenuEntity menu){
		//数据校验
		verifyForm(menu);
				
		sysMenuService.update(menu);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除菜单")
	@DeleteMapping("/{menuId}")
	@PreAuthorize("hasAuthority('sys:menu:delete')")
	@ApiOperation("删除菜单")
	public R delete(@ApiParam("菜单ID") @PathVariable("menuId") Long menuId){
		//判断是否有子菜单或按钮
		List<SysMenuEntity> menuList = sysMenuService.queryListParentId(menuId);
		if(menuList.size() > 0){
			return R.error("请先删除子菜单或按钮");
		}

		sysMenuService.deleteBatch(new Long[]{menuId});
		
		return R.ok();
	}
	
	/**
	 * 验证参数是否正确
	 */
	private void verifyForm(SysMenuEntity menu){
		if(StringUtils.isBlank(menu.getName())){
			throw new RRException("菜单名称不能为空");
		}
		
		if(menu.getParentId() == null){
			throw new RRException("上级菜单不能为空");
		}
		
		//菜单
		if(menu.getType() == MenuType.MENU.getValue()){
			if(StringUtils.isBlank(menu.getUrl())){
				throw new RRException("菜单URL不能为空");
			}
		}
		
		//上级菜单类型
		int parentType = MenuType.CATALOG.getValue();
		if(menu.getParentId() != 0){
			SysMenuEntity parentMenu = sysMenuService.queryObject(menu.getParentId());
			parentType = parentMenu.getType();
		}
		
		//目录、菜单
		if(menu.getType() == MenuType.CATALOG.getValue() ||
				menu.getType() == MenuType.MENU.getValue()){
			if(parentType != MenuType.CATALOG.getValue()){
				throw new RRException("上级菜单只能为目录类型");
			}
			return ;
		}
		
		//按钮
		if(menu.getType() == MenuType.BUTTON.getValue()){
			if(parentType != MenuType.MENU.getValue()){
				throw new RRException("上级菜单只能为菜单类型");
			}
			return ;
		}
	}
}
